package com.yy.wga.common.core.enmus;

import com.google.common.collect.Lists;
import lombok.AllArgsConstructor;
import lombok.Getter;

import java.math.BigDecimal;
import java.util.List;
import java.util.Objects;

/**
 * @Author: gc
 * @Date: 2022/5/24
 * @Description: 用户等级枚举
 **/
@AllArgsConstructor
@Getter
public enum EUserGrade {
    /**
     * 用户等级 0.普通用户 1.享客 2.服务商
     */
    SERVER_2(2, 2, new BigDecimal("8"), new BigDecimal("10"), Integer.MAX_VALUE, null, "V2服务商"),
    SERVER_1(2, 1, new BigDecimal("20"), new BigDecimal("10"), Integer.MAX_VALUE, EUserGrade.SERVER_2, "V1服务商"),
    SHARE_5(1, 5, new BigDecimal("5"), new BigDecimal("10"), 12_000_000, EUserGrade.SERVER_1, "V5享客"),
    SHARE_4(1, 4, new BigDecimal("5"), new BigDecimal("10"), 6000_000, EUserGrade.SHARE_5, "V4享客"),
    SHARE_3(1, 3, new BigDecimal("5"), new BigDecimal("10"), 3000_000, EUserGrade.SHARE_4, "V3享客"),
    SHARE_2(1, 2, new BigDecimal("5"), new BigDecimal("10"), 300_000, EUserGrade.SHARE_3, "V2享客"),
    SHARE_1(1, 1, new BigDecimal("40"), new BigDecimal("10"), 0, EUserGrade.SHARE_2, "V1享客"),
    COMMON(0, 0, BigDecimal.ONE, BigDecimal.ZERO, 0, EUserGrade.SHARE_1, "普通用户"),
    ;

    private final Integer type;
    /**
     * 等级
     */
    private final Integer level;
    /**
     * 自购/分销分佣比例,前面等级未分佣则由后面等级收取
     */
    private final BigDecimal buyCommissionRate;
    /**
     * 直推人分佣比例，收取直接下级订单收益固定比例佣金
     */
    private final BigDecimal directCommissionRate;
    /**
     * 销售额门槛
     */
    private final Integer salesThreshold;
    /**
     * 下一个等级
     */
    private final EUserGrade nextGrade;
    private final String desc;

    public static List<EUserGrade> getCommissionLink() {
        return Lists.newArrayList(SHARE_1, SHARE_2, SHARE_3, SHARE_4, SHARE_5, SERVER_1, SERVER_2);
    }

    public static EUserGrade getByType(Integer type, Integer level) {
        if (type != null) {
            for (EUserGrade value : EUserGrade.values()) {
                if (value.type.equals(type) && value.level.equals(level)) {
                    return value;
                }
            }
        }
        return null;
    }

    /**
     * 等级对比，大于参数等级返回1，小于返回-1，等于返回0
     *
     * @param userGrade
     * @return
     */
    public int compare(EUserGrade userGrade) {
        if (this.type > userGrade.type) {
            return 1;
        } else if (Objects.equals(this.type, userGrade.type)) {
            if (this.level > userGrade.level) {
                return 1;
            } else if (Objects.equals(this.level, userGrade.level)) {
                return 0;
            } else {
                return -1;
            }
        } else {
            return -1;
        }
    }
}
